package com.cola.action;

import com.cola.entity.User;
import com.cola.entity.courier.Courier;
import com.cola.entity.order.Order;
import com.cola.entity.order.OrderMessage;
import com.cola.enums.OrderType;
import com.cola.service.CourierService;
import com.cola.service.MepService;
import com.cola.service.MessageService;
import com.cola.service.ShopService;
import com.cola.utils.Struts2Utils;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Max
 * @since 2017/9/14
 */
public class MepAction extends BaseAction {
    private Integer begin_no;
    private Integer page_size;
    private String order_no;
    private Integer message_id=0;
    private String reason;
    private Integer courier_id;
    private String order_status;
    private String mobile;
    private String new_mobile;

    private String courier_name;

    /**
     * 经销商首页
     * @author Max
     * @since 2017/9/13
     */
    public String mepIndex(){
        return SUCCESS;
    }

    /**
     * 获取消息列表
     * @author Max
     * @since 2017/9/18
     */
    public void getOrderMessages(){
        Map<String,Object> resultMap = new HashMap<String,Object>();
        resultMap.put("result","error");
        User user = getUserInfoFromRedis();
        List<OrderMessage> list = MessageService.getInstance().getOrderMessages(user.getColaNo(),begin_no,page_size);
        if(list!=null){
            resultMap.put("result","success");
            resultMap.put("list",list);
        }
        Struts2Utils.renderJson(resultMap);
    }

    /**
     * 跳转到确认订单页面
     * @author Max
     * @since 2017/9/18
     */
    public String toCheckOrder(){
        User user = getUserInfoFromRedis();

        if(user.getUserId()!=null){
            Order order = ShopService.getInstance().getOrderInfoByOrderNo(order_no);

            if(order.getOrder_status().equals(OrderType.WAITING_ALLOCATION_COURIER.getValue())){

                List<Courier> list = CourierService.getInstance().getCourierList(user.getColaNo());

                setRequestAttribute("couriers", list);
            }

            if (order.getCourier_id()!=null && order.getCourier_id()>0){

                setRequestAttribute("courier_info", CourierService.getInstance().getCourierInfoById(order.getCourier_id()));
            }
            if(message_id>0){
                MessageService.getInstance().readOrderMessage(message_id);
                setRequestAttribute("message_id",message_id);
            }

            setRequestAttribute("order",order);
            setRequestAttribute("user",user);

            return SUCCESS;
        }
        return AUTH;
    }

    /**
     * 拒单
     * @author Max
     * @since 2017/9/18
     */
    public void rejectOrder(){
        User user = getUserInfoFromRedis();
        Map<String,Object> resultMap = new HashMap<String,Object>();
        resultMap.put("result","error");
        if(MepService.getInstance().rejectOrder(order_no,user.getColaNo(),reason,message_id)){
            resultMap.put("result","success");
        }
        Struts2Utils.renderJson(resultMap);
    }

    /**
     * 接单
     * @author Max
     * @since 2017/9/18
     */
    public void acceptOrder(){
        User user = getUserInfoFromRedis();
        Map<String,Object> resultMap = new HashMap<String,Object>();
        resultMap.put("result","error");
        if(MepService.getInstance().acceptOrder(order_no,user.getColaNo(),message_id)){
            resultMap.put("result","success");
        }
        Struts2Utils.renderJson(resultMap);
    }

    /**
     * 分配配送员
     * @author Max
     * @since 2017/9/19
     */
    public void setCourier(){
        Map<String,Object> resultMap = new HashMap<String,Object>();
        resultMap.put("result","error");
        //修改订单状态
        if(MepService.getInstance().setCourier(order_no,courier_id)){
            resultMap.put("result","success");
        }
        Struts2Utils.renderJson(resultMap);
    }


    //====================================订单=======================================

    /**
     * 跳转到经销商订单列表页面
     * @author Max
     * @since 2017/9/19
     */
    public String toMepOrderList(){
        User user = getUserInfoFromRedis();
        List<Courier> list = CourierService.getInstance().getCourierList(user.getColaNo());
        setRequestAttribute("couriers", list);
        setRequestAttribute("user",user);
        return SUCCESS;
    }


    /**
     * 获取经销商全部订单
     * @author Max
     * @since 2017/9/19
     */
    public void getMepOrderList(){
        User user = getUserInfoFromRedis();
        Map<String,Object> resultMap = new HashMap<String,Object>();
        //修改订单状态
        List<Order> list = MepService.getInstance().getMepAllOrderList(user.getColaNo());
        resultMap.put("result","success");
        resultMap.put("list",list);
        Struts2Utils.renderJson(resultMap);
    }


    //=======================================个人中心====================================


    /**
     * 跳转到个人中心页面
     * @author Max
     * @since 2017/9/18
     */
    public String toMepCenter(){
        User user = getUserInfoFromRedis();
        setRequestAttribute("user",MepService.getInstance().getMeoInfoByMepNo(user.getColaNo()));
        return SUCCESS;
    }

    /**
     * 跳转到我的信息
     * @author Max
     * @since 2017/9/19
     */
    public String toMepDetailInfo(){
        User user = getUserInfoFromRedis();
        setRequestAttribute("user",MepService.getInstance().getMeoInfoByMepNo(user.getColaNo()));
        return SUCCESS;
    }


    //=====================================配送员=========================================

    /**
     * 跳转到配送员页面
     * @author Max
     * @since 2017/9/19
     */
    public String toMepCouriers(){
        User user = getUserInfoFromRedis();
        List<Courier> list = CourierService.getInstance().getCourierList(user.getColaNo());
        setRequestAttribute("couriers", list);
        return SUCCESS;
    }

    /**
     * 跳转到添加配送员
     * @author Max
     * @since 2017/9/19
     */
    public String toAddMepCourier(){
        User user = getUserInfoFromRedis();
        return SUCCESS;
    }

    /**
     * 添加配送员信息
     * @author Max
     * @since 2017/9/19
     */
    public void saveCourier(){
        User user = getUserInfoFromRedis();
        Map<String,Object> resultMap = new HashMap<String,Object>();
        resultMap.put("result","error");
        if(CourierService.getInstance().checkCourierMobile(mobile)){

            if(CourierService.getInstance().saveCourierInfo(courier_name,user.getColaNo(),mobile)>0){
                resultMap.put("result","success");
            }
        }else{
            resultMap.put("msg","手机号已经存在！");
        }
        Struts2Utils.renderJson(resultMap);
    }


    /**
     * 跳转到配送员详情页面
     * @author Max
     * @since 2017/9/19
     */
    public String toCourierDetail(){
        setRequestAttribute("courier",CourierService.getInstance().getCourierInfoById(courier_id));
        return SUCCESS;
    }

    /**
     * 删除配送员
     * @author Max
     * @since 2017/9/19
     */
    public void deleteCourier(){
        User user = getUserInfoFromRedis();
        Map<String,Object> resultMap = new HashMap<String,Object>();
        resultMap.put("result","error");
        if(user !=null && user.getUserId()!=null && CourierService.getInstance().deleteCourierInfo(courier_id)){
            resultMap.put("result","success");
        }
        Struts2Utils.renderJson(resultMap);
    }

    /**
     * 跳转到编辑配送员详情页面
     * @author Max
     * @since 2017/9/19
     */
    public String toCourierEdit(){
        setRequestAttribute("courier",CourierService.getInstance().getCourierInfoById(courier_id));
        return SUCCESS;
    }

    /**
     * 删除配送员
     * @author Max
     * @since 2017/9/19
     */
    public void editCourier(){
        User user = getUserInfoFromRedis();
        Map<String,Object> resultMap = new HashMap<String,Object>();
        resultMap.put("result","error");
        if(StringUtils.isNoneEmpty(mobile) && StringUtils.isNoneEmpty(new_mobile)){
            if(!mobile.equals(new_mobile)){
                if(CourierService.getInstance().checkCourierMobile(mobile)){
                    if(user !=null && user.getUserId()!=null
                            && CourierService.getInstance().updateCourierInfo(courier_id,new_mobile,courier_name)){
                        resultMap.put("result","success");
                    }
                }else{
                    resultMap.put("msg","手机号已存在！");
                }
            }else{
                if(user !=null && user.getUserId()!=null
                        && CourierService.getInstance().updateCourierInfo(courier_id,new_mobile,courier_name)){
                    resultMap.put("result","success");
                }
            }
        }else{
            resultMap.put("msg","修改配送员信息异常！");
        }

        Struts2Utils.renderJson(resultMap);
    }


    public Integer getBegin_no() {
        return begin_no;
    }

    public void setBegin_no(Integer begin_no) {
        this.begin_no = begin_no;
    }

    public Integer getPage_size() {
        return page_size;
    }

    public void setPage_size(Integer page_size) {
        this.page_size = page_size;
    }

    public String getOrder_no() {
        return order_no;
    }

    public void setOrder_no(String order_no) {
        this.order_no = order_no;
    }

    public Integer getMessage_id() {
        return message_id;
    }

    public void setMessage_id(Integer message_id) {
        this.message_id = message_id;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public Integer getCourier_id() {
        return courier_id;
    }

    public void setCourier_id(Integer courier_id) {
        this.courier_id = courier_id;
    }

    public String getOrder_status() {
        return order_status;
    }

    public void setOrder_status(String order_status) {
        this.order_status = order_status;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getCourier_name() {
        return courier_name;
    }

    public void setCourier_name(String courier_name) {
        this.courier_name = courier_name;
    }

    public String getNew_mobile() {
        return new_mobile;
    }

    public void setNew_mobile(String new_mobile) {
        this.new_mobile = new_mobile;
    }
}
